import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from './utils/get-page-title'
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/404'] // 白名单

router.beforeEach(async(to, from, next) => {
  NProgress.start()
  // console.log(to, from)
  // console.log('next', next)
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // . 获取个人信息
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      document.title = getPageTitle(to.meta.title)
      next()
    }
  } else {
    if (whiteList.includes(to.path)) return next()
    next(`/login?redirect=${to.path}`)
    NProgress.done()
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
